﻿local textDisplay=textCreateDisplay()
local textItem=textCreateTextItem("ZGON!",0.5,0.3,2,50,150,50,150,4,"center","center",100)
textDisplayAddText(textDisplay,textItem)
for k,v in ipairs(getElementsByType("player"))do
	removeElementData(v,"butelka")
	removeElementData(v,"kielichy")
	setElementData(v,"wytrzymalosc",15)
	fadeCamera(v,true)
	setCameraTarget(v,v)
end
addEventHandler("onPlayerJoin",root,
	function()
		removeElementData(source,"butelka")
		removeElementData(source,"kielichy")
		setElementData(source,"wytrzymalosc",15)
	end
)
addEventHandler("onPlayerQuit",root,
	function()
		if getElementData(source,"butelka") then
			destroyElement(getElementData(source,"butelka"))
		end
	end
)
karetki={createVehicle(416,-2312.8466796875,-1685.40625,482.43707275391,0.54931640625,354.80895996094,170.95275878906),createVehicle(416,-2316.6767578125,-1682.9775390625,482.6598815918,2.6806640625,354.60571289063,173.19946289063),createVehicle(416,-2320.6982421875,-1679.279296875,482.94467163086,2.57080078125,354.60021972656,173.14453125)}
setTimer(
	function()
		for k,v in ipairs(karetki)do
			setVehicleFrozen(v,true)
			setVehicleDamageProof(v,true)
			setVehicleOverrideLights(v,1)
			setVehicleEngineState(v,false)
			local ped=createPed(math.random(274,276),0,0,0)
			warpPedIntoVehicle(ped,v)
			setElementData(ped,"karetka",v)
		end
	end
,2000,1)
addEventHandler("onPedDeath",root,
	function()
		if getElementData(source,"karetka") then
			local karetka=getElementData(source,"karetka")
			destroyElement(source)
			local ped=createPed(math.random(274,276),0,0,0)
			warpPedIntoVehicle(ped,karetka)
			addElementData(ped,"karetka",karetka)
		end
	end
)
barMarker1=createMarker(-2351.5124511719,-1612.1759033203,482.73544311523,"cylinder",1,100,0,255,150)
barMarker2=createMarker(-2351.0051269531,-1610.4366455078,482.73544311523,"cylinder",1,100,0,255,150)
butelkiID={1486,1543,1544,1520,1664}
butelki={{-0.08,0.03,0.1,0,90,0},{-0.3,0.02,0.1,0,90,0},{-0.3,0.01,0.1,0,90,0},{-0.25,0.023,0.1,0,90,0},{-0.1,0.04,0.1,0,90,-10}}
function chlanie(gracz)
	local kielichy=getElementData(gracz,"kielichy")
	if kielichy then
		if kielichy ~= 0 then
			unbindKey(gracz,"mouse1","down",chlanie)
			unbindKey(gracz,"mouse2","down",wyrzucenie)
			setElementData(gracz,"kielichy",kielichy - 1)
			setPedAnimation(gracz,"VENDING","VEND_Drink_P",-1,false,true,false,false)
			local wytrzymalosc=getElementData(gracz,"wytrzymalosc")-1
			setElementData(gracz,"wytrzymalosc",wytrzymalosc)
			setTimer(
				function()
					if wytrzymalosc==5 then
						setPedAnimation(gracz,"MISC","plyr_shkhead",-1,false,true,false,false)
						outputChatBox("* Oj, trochę się napiłeś!",gracz,50,150,50)
						setTimer(
							function()
								bindKey(gracz,"mouse1","down",chlanie)
								bindKey(gracz,"mouse2","down",wyrzucenie)
							end
						,1300,1)
					elseif wytrzymalosc <= 0 then
						setPedAnimation(gracz,"HEIST9","CAS_G2_GasKO",-1,false,false,false,true)
						setTimer(setPedAnimation,3000,1,gracz,"CRACK","crckdeth1",-1,false,false,false,true)
						setElementData(gracz,"wytrzymalosc",15)
						setTimer(zgon,3700,1,gracz)
					else
						bindKey(gracz,"mouse1","down",chlanie)
						bindKey(gracz,"mouse2","down",wyrzucenie)
					end
				end
			,1400,1)
		else
			wyrzucenie(gracz)
		end
	end
end
function zgon(gracz)
	toggleAllControls(gracz,false,true,false)
	local x,y,z=getElementPosition(gracz)
	fadeCamera(gracz,false,5)
	local z2=z+1
	setTimer(
		function()
			setCameraMatrix(gracz,x,y,z2,x,y,z)
			z2=z2+0.7
			textDisplayAddObserver(textDisplay,gracz)
		end
	,700,3)
	setTimer(
		function()
			local id=math.random(1,3)
			local karetka=karetki[id]
			warpPedIntoVehicle(gracz,karetka,math.random(2,3))
			setCameraTarget(gracz,gracz)
			textDisplayRemoveObserver(textDisplay,gracz)
			fadeCamera(gracz,true,1)
		end
	,5000,1)
	setTimer(
		function()
			toggleAllControls(gracz,true,true,false)
			setControlState(gracz,"enter_exit",true)
			local butelka=getElementData(gracz,"butelka")
			removeElementData(gracz,"butelka")
			removeElementData(gracz,"kielichy")
			destroyElement(butelka)
			outputChatBox("* Zaliczyłeś zgon, ale już jest okej. Wracaj do zabawy!",gracz,50,150,50)
		end
	,5700,1)	
end
function wyrzucenie(gracz)
	if(getElementData(gracz,"butelka"))then
		setPedAnimation(gracz,"MISC","KAT_Throw_K",800,false,true,false,false)
		removeElementData(gracz,"kielichy")
		local butelka=getElementData(gracz,"butelka")
		removeElementData(gracz,"butelka")
		setTimer(destroyElement,800,1,butelka)
		toggleControl(gracz,"fire",true)
		unbindKey(gracz,"mouse1","down",chlanie)
		unbindKey(gracz,"mouse2","down",wyrzucenie)
	end
end
function dodawanieButelki(gracz,id)
	if (not id)or(id<1)or(id>5) then
		id=math.random(1,5)
	end
	local butelka=createObject(butelkiID[id],0,0,0)
	exports.bone_attach:attachElementToBone(butelka,gracz,11,butelki[id][1],butelki[id][2],butelki[id][3],butelki[id][4],butelki[id][5],butelki[id][6])
	setElementData(gracz,"kielichy",math.random(4,7))
	setElementData(gracz,"butelka",butelka)
	toggleControl(gracz,"fire",false)
	bindKey(gracz,"mouse1","down",chlanie)
	bindKey(gracz,"mouse2","down",wyrzucenie)
end
addCommandHandler("chlanko",
	function(gracz)
		if(getElementData(gracz,"kielichy"))then
			outputChatBox("* Musisz wyrzucić butelkę, którą masz w ręku! (kliknij prawym przyciskiem myszy)",gracz,50,150,50)
		else
			dodawanieButelki(gracz)
		end
	end
)
function handleMarkers(element)
	if (source == barMarker1) or (source == barMarker2) then
		if getElementType(element) == "player" then
			if (getElementData(element,"kielichy")) then
				outputChatBox("* Musisz wyrzucić butelkę, którą masz w ręku! (kliknij prawym przyciskiem myszy)",element,50,150,50)
			else
				outputChatBox("* Na zdrowie! :)",element,50,150,50)
				dodawanieButelki(element)
			end
		end
	end
end
addEventHandler("onMarkerHit", root, handleMarkers)